<template>
  <main>
    <ul class="clear">
      <li v-for="(item, index) in list" :key="index" class="=" clear>
        <div class="img">
          <img :src="item.img | img_res" alt="" />
        </div>
        <div class="film_name">
          <h2>{{ item.nm }}</h2>
          <div class="sc">
            点评:<b>{{ item.sc }}</b>
          </div>
          <p>主演:{{ item.star }}</p>
          <p>{{ item.showInfo }}</p>
          <button class="btn-blue" v-if="item.showst == 4">预售</button>
          <button class="btn-red" v-else>购买</button>
        </div>
      </li>
    </ul>
  </main>
</template>

<script>
export default {
  props: ["num"],
  data() {
    return {
      list: [],
      newlist: [],
      ids: [], //获取所有电影的ids
      startids: 0, //记录上一次的数据
      length: 6, //一次获取的数量
    };
  },
  filters: {
    img_res(i) {
      // console.log(i);
      const reg = new RegExp("w.h", "ig");
      return i.replace(reg, "64.90");
    },
  },
  created() {
    fetch("http://www.pudge.wang:3080/api/movies/list")
      .then((response) => response.json())
      .then(async (res) => {
        console.log(res);
        if (res.status == 0) {
          this.list = res.result;
          this.startids = res.result.length;
          this.ids = res.ids;
          await this.$nextTick();
          this.$emit("add");
        }
      });
  },
  watch: {
    num() {
      console.log("aaa");
      const arr = this.ids.slice(this.startids, this.startids + this.length);
      const newids = arr.join(",");
      console.log(arr);
      fetch("http://www.pudge.wang:3080/api/movies/more", {
        method: "POST",
        body: JSON.stringify({
          ids: newids,
        }),
        headers: {
          "content-type": "application/json",
        },
      })
        .then((response) => response.json())
        .then(async (res) => {
          if (res.status === 0) {
            console.log(res);
            this.list = [...this.list, ...res.result];
            this.startids = this.startids + this.length;
            this.newlist = res.result;
            console.log(this.newlist);
            await this.$nextTick();
            this.$emit("finish");
          }
        });
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../assets/css/var.less";
main {
  width: 359px;
  background-color: #fff;
}
li {
  width: 344px;
  height: 114px;
  // margin-bottom: 10px;

  margin-left: 15px;

  h2 {
    font-size: 16px;
    color: #333;
    font-weight: 700;
    padding-right: 5px;
  }
  p {
    font-size: 12px;
  }
  .sc {
    font-size: 14px;
    b {
      color: #faaf00;
    }
  }
  .img {
    float: left;
    width: 64px;
    height: 90px;
    margin-right: 10px;
    // padding: 12px 14px 12px 0;
    margin: 10px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.film_name {
  width: 256px;
  height: 90px;
  padding: 12px 14px 12px 0;
  // margin-left: 74px;
  height: 90px;
  max-height: 90px;
  position: relative;
  border-bottom: 1px solid #ccc;
  float: left;
  .btn-blue {
    position: absolute;
    right: 10px;
    bottom: 50%;
    width: 47px;
    height: 27px;
    line-height: 28px;
    text-align: center;
    box-sizing: border-box;
    // background-color: #f03d37;
    color: #fff;
    border-radius: 4px;
    white-space: nowrap;
    font-size: 12px;
    cursor: pointer;
    background-color: blue;
  }
  .btn-red {
    position: absolute;
    right: 10px;
    bottom: 50%;
    width: 47px;
    height: 27px;
    line-height: 28px;
    text-align: center;
    box-sizing: border-box;
    // background-color: #f03d37;
    color: #fff;
    border-radius: 4px;
    white-space: nowrap;
    font-size: 12px;
    cursor: pointer;
    background-color: @bg-color;
  }
}
</style>
